package com.shyb.lingxian.mine.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shyb.lingxian.MainActivity;
import com.shyb.lingxian.R;
import com.shyb.lingxian.Util.AppUtil;
import com.shyb.lingxian.Util.StringUtils;
import com.shyb.lingxian.Util.ToastUtil;
import com.shyb.lingxian.base.BaseActivity;
import com.shyb.lingxian.mine.presenter.LoginPresenter;
import com.shyb.lingxian.mine.view.LoginView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by gs on 2018/3/23.
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginView,View.OnClickListener{

    private TextView getCode;
    private EditText phone;
    private EditText logincode;
    /**
     * handler 计时器what值
     */
    private final int TIMER_WHAT = 100;
    /**
     * 计时器倒计时最大值
     */
    private final int TIME_MAX_WAIT = 60;
    private Handler mHandler;
    private int count;
    private TextView xieyi;
    private Button commit;
    private TextView kefuPhone;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initHeadTitle("登录/注册");

        initViews();


    }

    private void initViews() {
        getCode = (TextView) findViewById(R.id.getcode);
        phone = (EditText) findViewById(R.id.loginphone);
        logincode = (EditText) findViewById(R.id.logincode);
        xieyi = (TextView) findViewById(R.id.xieyi);
        commit = (Button) findViewById(R.id.commit);
        kefuPhone = (TextView) findViewById(R.id.kefuPhone);


        getCode.setOnClickListener(this);
        xieyi.setOnClickListener(this);
        commit.setOnClickListener(this);
        kefuPhone.setOnClickListener(this);

    }

    @Override
    protected LoginPresenter initPresenter() {
        return new LoginPresenter( this);
    }

    @Override
    protected void initialize() {
        initData();
        //获取ticket
        presenter.getTicket(this);
    }

    @Override
    public void onError(String message) {
        ToastUtil.showToast(this,message);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.getcode:
                presenter.getCOde(this);

                break;

            case R.id.commit:
                presenter.Login(this);

                break;

            case R.id.xieyi:
                startActivity(new Intent(this,XieYiWebVActivity.class));
                break;

            case R.id.kefuPhone:

                String kefuPhone = this.kefuPhone.getText().toString();
                if (kefuPhone.length() >0){
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + kefuPhone));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }

                break;
        }
    }

    @Override
    public String getPhone() {
        String trimphone = phone.getText().toString().trim();
        if (StringUtils.isBlank(trimphone)){
            ToastUtil.showToast(this,"您的手机号码不为空！");
        } else if (!AppUtil.isPhoneNumber(trimphone)){
            ToastUtil.showToast(this,"您的手机号码不正确，请重新录入！");
            return "";
        }

        return trimphone;
    }

    @Override
    public void getCodeSucess() {
        startCount();
    }

    @Override
    public String getCode() {
        String trimcode = logincode.getText().toString().trim();
        if (trimcode.length() == 0){
            ToastUtil.showToast(this,"验证码不为空！");
            return "";
        }
        return trimcode;
    }

    @Override
    public void LoginSuccess() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    public void startCount() {
        count = TIME_MAX_WAIT;
        final Timer timer = new Timer();
        TimerTask timeTask = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(TIMER_WHAT);
                if (count <= 0) {
                    timer.cancel();
                }
            }
        };
        timer.schedule(timeTask, 1000, 1000);
    }

    @SuppressLint("HandlerLeak")
    private void initData(){
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                {
                    switch (msg.what) {
                        case TIMER_WHAT:
                            if (count <= 0) {
                                getCode.setText(R.string.verify_code_resend);
                            } else {
                                count--;
                                getCode.setText(String.format(getResources().getString(R.string.verify_code_wait), count));
                            }
                            break;
                    }

                }
            }
        };

    }
}
